﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI;
using System.Text;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:BingSearch runat=server></{0}:BingSearch>")]
    public class BingSearch : WebControl
    {

        public Unit SearchBoxWidth { get { object obj = ViewState["SearchBoxWidth"]; if (obj == null) { return new Unit(322, UnitType.Pixel); } return (Unit)obj; } set { ViewState["SearchBoxWidth"] = value; } }
        public Unit ResultBoxWidth { get { object obj = ViewState["ResultBoxWidth"]; if (obj == null) { return new Unit(600, UnitType.Pixel); } return (Unit)obj; } set { ViewState["ResultBoxWidth"] = value; } }
        public Unit ResultBoxHeight { get { object obj = ViewState["ResultBoxHeight"]; if (obj == null) { return new Unit(400, UnitType.Pixel); } return (Unit)obj; } set { ViewState["ResultBoxHeight"] = value; } }
        public BingSearchTheme SearchTheme { get { object obj = ViewState["BingSearchTheme"]; if (obj == null) { return BingSearchTheme.Blue; } return (BingSearchTheme)obj; } set { ViewState["BingSearchTheme"] = value; } }
        public string Market { get { object obj = ViewState["Market"]; if (obj == null) { return "en-GB"; } return obj.ToString(); } set { ViewState["Market"] = value; } }
        public string Title { get { object obj = ViewState["Title"]; if (obj == null) { return "Web"; } return obj.ToString(); } set { ViewState["Title"] = value; } }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<meta name=\"Search.WLSearchBox\" content=\"1.1, en-GB\" />");
            writer.Write("<div id=\"WLSearchBoxDiv\">");
            writer.Write("<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:" + this.SearchBoxWidth.ToString() + "; height:32px;\"><tr id=\"WLSearchBoxPlaceholder\"><td style=\"width:100%; border:solid 1px #cccccc; border-right-style:none; padding-left:10px; padding-right:10px; vertical-align:middle;\"><input id=\"WLSearchBoxInput\" type=\"text\" value=\"&#x4c;&#x6f;&#x61;&#x64;&#x69;&#x6e;&#x67;&#x2e;&#x2e;&#x2e;\" disabled=\"disabled\" style=\"background-image:url(http://www.bing.com/siteowner/s/siteowner/searchbox_background_k.png); background-position:right; background-repeat:no-repeat; font-family:Arial; font-size:14px; color:#000000; width:100%; border:none 0 transparent;\"/></td><td style=\"border:solid 1px #cccccc; border-left-style:none; padding-left:0px; padding-right:3px;\"><input id=\"WLSearchBoxButton\" type=\"image\" src=\"http://www.bing.com/siteowner/s/siteowner/searchbutton_normal_k.gif\" style=\"border:none 0 transparent; height:24px; width:24px; vertical-align:top;\"/></td></tr></table>");
            writer.Write("<script type=\"text/javascript\" charset=\"utf-8\">");
            writer.Write("var WLSearchBoxConfiguration=");
            writer.Write("{");
            writer.Write("\"global\":{");
            writer.Write("\"serverDNS\":\"www.bing.com\",");
            writer.Write("\"market\":\"" + Market + "\"");
            writer.Write("},");
            writer.Write("\"appearance\":{");
            writer.Write("\"autoHideTopControl\":false,");
            writer.Write("\"width\":" + this.ResultBoxWidth.Value.ToString() + ",");
            writer.Write("\"height\":" + this.ResultBoxHeight.Value.ToString() + ",");

            switch (SearchTheme)
            {
                case BingSearchTheme.Blue:
                    writer.Write("\"theme\":\"Blue\"");
                    break;
                case BingSearchTheme.Green:
                    writer.Write("\"theme\":\"Green\"");
                    break;
                case BingSearchTheme.Red:
                    writer.Write("\"theme\":\"Red\"");
                    break;
                case BingSearchTheme.Yellow:
                    writer.Write("\"theme\":\"Yellow\"");
                    break;
            }

            writer.Write("},");
            writer.Write("\"scopes\":[");
            writer.Write("{");
            writer.Write("\"type\":\"web\","); 
            if (string.IsNullOrEmpty(this.Title))
            {
                writer.Write("\"caption\":\"Web\",");
            }
            else
            {
                writer.Write("\"caption\":\"" + this.Title + "\",");
            }

            writer.Write("\"searchParam\":\"\"");
            writer.Write("}");
            writer.Write("]");
            writer.Write("}");
            writer.Write("</script>");
            writer.Write("<script type=\"text/javascript\" charset=\"utf-8\" src=\"http://www.bing.com/bootstrap.js?market=" + this.Market + "&ServId=SearchBox&ServId=SearchBoxWeb&Callback=WLSearchBoxScriptReady\"></script>");
            writer.Write("</div>");
        }
    }

    public enum BingSearchTheme
    {
        Blue,
        Yellow,
        Green,
        Red
    }

}
